<?php


namespace app\cuisine\controller;


use app\cuisine\model\NewsModel;
use app\cuisine\validate\NewsValidate;
use cmf\controller\AdminBaseController;
use think\Request;

class NewsController extends AdminBaseController
{
    //显示页面
    public function index(){
        //获取搜索字段
        $key = input('key');
        $where = [];
        //判断是否有值
        if($key){
            $where[] = ['author|title', 'like', '%' . $key . '%'];
        }
        $list = NewsModel::where($where)->order('list_order','DESC')->paginate(10);
        return $this->fetch('index',[
            'list'=>$list
        ]);
    }
    //排序
    public function Orders()
    {
        $model = new NewsModel();
        parent::listOrders($model); // TODO: Change the autogenerated stub
        $this->success('排序成功');
    }
    //添加
    public function add(){
        $list = NewsModel::select();
        return $this->fetch('add',[
            'list' => $list
        ]);
    }
    public function addPost(){
        //获取数据
        $data = input('post.');
        //方式1
        $content1 = htmlspecialchars_decode($data['content']);
        $content2 = cmf_replace_content_file_url($content1,true);
        $content3 = htmlspecialchars($content2);
        $data['content'] = $content3;
        //方式2
        //$data['content'] = htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($data['content']),true));
        //验证器
        $validate = new NewsValidate();
        $result = $validate->scene('add')->check($data);
        //判断验证器true
        if(!$result){
            $this->error($validate->getError());
        }
        //更新时间
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['update_time'] = date('Y-m-d H:i:s');
        //入库 判断成功
        if(NewsModel::create($data)){
            $this->success('添加成功','News/index');
        }else{
            $this->error('添加失败');
        }
    }
    //编辑
    public function edit(){
        $id = input('id');
        $list = NewsModel::find($id);
        $list['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($list['content']),true);
        return $this->fetch('edit',[
            'list' => $list
        ]);
    }
    public function editPost(){
        $data = input('post.');
        //方式1
        $content1 = htmlspecialchars_decode($data['content']);
        $content2 = cmf_replace_content_file_url($content1,true);
        $content3 = htmlspecialchars($content2);
        $data['content'] = $content3;
        //验证器
        $validate = new NewsValidate();
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        //查询
        $list = NewsModel::find($data['id']);
        if(!$list){
            $this->error('未查询的记录');
        }
        $list->update_time = date('Y-m-d H:i:s');
        if($list->update($data)){
            $this->success('保存成功','News/index');
        }else{
            $this->error('保存失败');
        }
    }
    //单条删除
    public function delete(){
        $id = input('id/d','');
        $isExist = NewsModel::find($id);
        //判断该id是否存在
        if(!$isExist){
            $this->error('该数据不存在');
        }

        $del = NewsModel::destroy($id);
        if($del){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    // 查看某个文章的详情
    public function detail(){
        $id =input('id/d');
        if(empty($id)){
            return '参数错误';
        }
        $news = NewsModel::find($id);
        $news['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($news['content']),true);
        $this->assign('list',$news);
        return $this->fetch('detail');
    }
    //显示隐藏内容
    public function isShow(Request $request){
        $id = $request->param('id');
        if(!$id){
            $this->error('未查询到该id');
        }
//        $switch = input('switch');
        $news = NewsModel::find($id);
        if($news['switch'] == 1){
            $news['switch'] = 0;
        }
        if($news['switch'] == 0){
            $news['switch'] = 1;
        }
        if($news->update(['switch'=>$news['switch']],['id'=>$id])){
            $this->success('修改成功','News/index');
        }else{
            $this->error('修改失败');
        }
    }
    //回收站
    public function recycle(){
        //获取搜索字段
        $key = input('key');
        $where = [];
        //判断是否有值
        if($key){
            $where[] = ['author|title', 'like', '%' . $key . '%'];
        }
        $list = NewsModel::order(['list_order DESC'])
            ->onlyTrashed()
            ->where($where)
            ->paginate(10);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function recover(){
        $id = input('id');
        if(!$id){
            $this->error('未查询到该数据');
        }
        $list = NewsModel::onlyTrashed()->find($id);
        if($list->restore()){
            $this->success('数据已恢复','News/index');
        }else{
            $this->error('数据恢复失败');
        }
    }
    //彻底删除
    public function CompletelyDelete()
    {
        $ids = input('id', '');
        if (empty($ids)) {
            $this->error('请至少选择一项');
        }
        $del = NewsModel::destroy($ids, true);
        if ($del) {
            $this->success('删除成功', 'News/recycle');
        } else {
            $this->error('删除失败');
        }
    }
}